package scau.erps.view.dashboard;

import java.awt.Font;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import scau.erps.app.Erps;
import scau.erps.entity.TeamOrder;
import scau.erps.service.TeamOrderService;
import scau.erps.service.impl.TeamOrderServiceImpl;

/**
 * 市场份额饼状图
 * @author DCJ
 * 
 */
public class ShareChart{	
	/**
	 * 得到图片
	 * @return
	 */
	public static JFreeChart getChart(){
		JFreeChart chart = createChart(createDataset());
		return chart;
	}
	/**
	 * 数据
	 */
	public static PieDataset createDataset(){
	     DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
	     TeamOrderService teamOrderService=Erps.getIocInstance().get(TeamOrderServiceImpl.class);
	     int[] data=new int[8];
	     for (int i=0;i<=7;i++){
	    	 data[i]=0;
	    	 List<TeamOrder> list=teamOrderService.getTeamOrderByStatus(i+1, "已收款");
	    	 for (TeamOrder teamOrder:list){
	    		 data[i]=data[i]+teamOrder.getTotal();
	    	 }
	    	 defaultpiedataset.setValue("第"+(i+1)+"组",data[i]);
	     }     
	     return defaultpiedataset;
	   }
	/**
	 * 绘制饼图
	 */
	public static JFreeChart createChart(PieDataset piedataset){
	     //定义图表对象
	     JFreeChart jfreechart = ChartFactory.createPieChart("市场占有率",piedataset,true,true,false);
	     //获得图表显示对象
	     PiePlot pieplot = (PiePlot)jfreechart.getPlot();
	     //设置图表标签字体
	     pieplot.setLabelFont(new Font("SansSerif",Font.BOLD,12));
	     pieplot.setNoDataMessage("No data available");
	     pieplot.setCircular(true);
	     pieplot.setLabelGap(0.01D);//间距	     
	     return jfreechart;
	   }
	}
